﻿<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                version="1.0">
  <xsl:key name="groups" match="//xsd:group" use="@name" />

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="//xsd:group[@ref]">
    <xsd:choice>
      <xsl:if test="@minOccurs">
        <xsl:attribute name="minOccurs">
          <xsl:value-of select="@minOccurs" />
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="@maxOccurs">
        <xsl:attribute name="maxOccurs">
          <xsl:value-of select="@maxOccurs" />
        </xsl:attribute>
      </xsl:if>
      <xsl:apply-templates select="key('groups', @ref)/xsd:choice/*" />
    </xsd:choice>
  </xsl:template>

  <xsl:template match="//xsd:group[@name]" />
</xsl:stylesheet>